<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2012-04-12 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'servlet/module/master.mod.php';

class moduleObj extends masterObj
{
	function __construct($request)
	{
		parent::__construct($request);
		
		//require_once ROOT_PATH.'include/logic/image.logic.php';
		
		$this->excute();
	}
	
	private function excute()
	{
		switch($this->act)
		{
			case 'list':
				$this->getList();
				break;
			case 'info':
				$this->getInfo();
				break;
			case 'do_favorite':
				$this->doFavorite();
				break;
			default:
				$this->message('g_04', STATUS_E);
				break;
		}
		
		$this->showResponse();
	}

	private function getList()
	{
		//DBFunction::getInstance()->setDebug();
		
		if(isset($this->request['latitude']) && isset($this->request['longitude']))
		{
			list($lat1, $lng1, $lat2, $lng2) = get_location_range($this->request['latitude'], $this->request['longitude']);
			$this->request['latitude1'] = $lat1;
			$this->request['longitude1'] = $lng1;
			$this->request['latitude2'] = $lat2;
			$this->request['longitude2'] = $lng2;
		}
		
		$result = get_scenic_list($this->request);
		
		if(!check_array($result['result'])) $this->message('sc_01', STATUS_F);
		
		foreach($result['result'] as &$temp)
		{
			// get distance from my place
			if(isset($this->request['latitude']) && isset($this->request['longitude']))
			{
				$temp['distance'] = get_distance($this->request['latitude'], $this->request['longitude'], $temp['latitude'], $temp['longitude']);
			}
			
			$temp['image'] = ($temp['image'] != '') ? BASE_URL.$temp['image'] : '';
			$temp['image_s'] = str_replace('_o.', '_s.', $temp['image']);
			
			$temp['notice'] = cut_string($temp['notice'], 140);
			$temp['desc'] = cut_string($temp['desc'], 140);
			
			$temp['create_date'] = date('Y-m-d', $temp['create_date']);
		}
		
		if(isset($this->request['latitude']) && isset($this->request['longitude']))
		{
			usort($result['result'], 'sort_by_dist_asc');
		}
		
		$this->response['status'] = STATUS_S;
		$this->response['count'] = (string)count($result['result']);
		$this->response['list'] = $result['result'];
	}
	
	private function getInfo()
	{
		//DBFunction::getInstance()->setDebug();
		
		$id = isset($this->request['sid']) ? $this->request['sid'] : '';
		
		if(!$id)
		{
			$this->message('g_05', STATUS_E);
		}
		
		$result = get_scenic_info($this->request);
		
		if(!isset($result['id']) || $result['id'] < 1)
		{
			$this->message('sc_01', STATUS_E);
		}
		
		//update_scenic_favorite_count(array('id'=>$id));
		
		if(check_array($result['image_list']))
		{
			foreach($result['image_list'] as &$temp)
			{
				if($temp['image'])
				{
					$temp['image'] = BASE_URL.$temp['image'];
					$temp['image_s'] = str_replace('_o.', '_s.', $temp['image']);
				}
			}
		}
		
		if($result['image']) $result['image'] = BASE_URL.$result['image'];
		$result['image_s'] = str_replace('_o.', '_s.', $result['image']);
		$result['comment_count'] = get_comment_count(array('id'=>$id));
		$result['create_date'] = date('Y-m-d', $result['create_date']);
		
		$this->response['status'] = STATUS_S;
		$this->response['info'] = $result;
	}
	
	private function doFavorite()
	{
		//DBFunction::getInstance()->setDebug();
		
		$id = isset($this->request['sid']) ? $this->request['sid'] : '';
		$uid = isset($this->request['uid']) ? $this->request['uid'] : '';
		
		if(!$id)
		{
			$this->message('g_05', STATUS_E);
		}
		
		$result = get_scenic_info($this->request);
		
		if(!isset($result['id']) || $result['id'] < 1)
		{
			$this->message('sc_01', STATUS_E);
		}
		
		$visitor = get_visitor(array('id'=>$uid));
		
		if(!isset($visitor['id']) || $visitor['id'] < 1)
		{
			$this->message('g_10', STATUS_E);
		}
		
		$result = check_up_down_records_exists(array('uid'=>$uid, 'item_id'=>$result['id'], 'type'=>'scenic'));
		if($result) $this->message('g_07', STATUS_F);
		
		$result = update_scenic_favorite_count(array('id'=>$id, 'uid'=>$uid));
		
		if($result)
		{
			$this->response['status'] = STATUS_S;
			$this->response['message_code'] = 'g_06';
		}
		else
		{
			$this->response['status'] = STATUS_F;
			$this->response['message_code'] = 'g_07';
		}
	}
}